package com.sxm.yiwei.fragments;


import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.FWBookContentActivity;
import com.sxm.yiwei.activitis.FWEditServiseContentActivity;
import com.sxm.yiwei.activitis.FWSellServiceActivity;
import com.sxm.yiwei.activitis.RangingResultPriceActivity;
import com.sxm.yiwei.activitis.ShopSetImageViewActivity;
import com.sxm.yiwei.activitis.SlideListView2;
import com.sxm.yiwei.adapters.SellServiceListAdapter;
import com.sxm.yiwei.beans.FWBook;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/** 服务 出售中
 * A simple {@link Fragment} subclass.
 */
public class SellSerivceFragment extends Fragment {

    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private String shopid;
    private List<FWBook.DataBean> list;
    private SlideListView2 selllist;
    private SellServiceListAdapter adapter;
    private ImageView nodata;

    public SellSerivceFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_sell_serivce, container, false);
        sharedPreferences = getActivity().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        shopid = sharedPreferences.getString("shopid", "");
        selllist = (SlideListView2) view.findViewById(R.id.sellservice_list);
        nodata = (ImageView) view.findViewById(R.id.servicer_nodata);
        list = new ArrayList<>();
        getSellList();


        return view;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            return;
        }else {
            list.clear();
            getSellList();
        }
    }


    // 获取店铺出售中的服务列表
    private void getSellList() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETSHOPSERVICELIST);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("shopId",shopid);
        object.put("status","1");
        params.addParameter("params",object.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject obj1 = JSON.parseObject(result);
                String data = obj1.getString("data");
                JSONArray values = JSON.parseArray(data);
                if (values.size()==0){

                }else {
                    nodata.setVisibility(View.GONE);
                    for (int i = 0; i < values.size(); i++) {

                        FWBook.DataBean dataBean = new FWBook.DataBean();
                        JSONObject obj2 = JSON.parseObject(String.valueOf(values.get(i)));
                        String amount = obj2.getString("amount");
                        String id = obj2.getString("id");
                        String intro = obj2.getString("intro");
                        String name = obj2.getString("name");
                        String serviceAvatar = obj2.getString("serviceAvatar");
                        String volume = obj2.getString("volume");
                        dataBean.setAmount(Integer.parseInt(amount));
                        dataBean.setId(id);
                        dataBean.setIntro(intro);
                        dataBean.setName(name);
                        dataBean.setVolume(Integer.parseInt(volume));
                        dataBean.setServiceAvatar(serviceAvatar);
                        list.add(dataBean);

                    }
                }

                adapter = new SellServiceListAdapter(getActivity(), list);
                selllist.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                adapter.setOnClickListenerEditOrDelete(new SellServiceListAdapter.OnClickListenerEditOrDelete() {
                    @Override
                    public void OnClickListenerEdit(int position) {
                        FWBook.DataBean dataBean1 = adapter.list.get(position);
                        String serviceid = dataBean1.getId();
                        Intent intent = new Intent(getActivity(), FWSellServiceActivity.class);
                        intent.putExtra("id",serviceid);
                        intent.putExtra("from","bianji");
                        startActivity(intent);
                        try {
                            selllist.turnToNormal();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void OnClickListenerDelete(int position) {
                        FWBook.DataBean dataBean1 = adapter.list.get(position);
                        deleteServiceItem(dataBean1.getId(),position);
                    }
                    @Override
                    public void OnClickListenersetView(int position) {
                        FWBook.DataBean item = (FWBook.DataBean) adapter.getItem(position);

                        // 设置展位
                        Intent i = new Intent(getActivity(), ShopSetImageViewActivity.class);
                        i.putExtra("serviceid",item.getId());
                        startActivity(i);
                        adapter.notifyDataSetChanged();
                        try {
                            selllist.turnToNormal();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                selllist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        FWBook.DataBean dataBean = (FWBook.DataBean) adapter.getItem(position);
                        String id1 = dataBean.getId();
                        Intent intent = new Intent(getActivity(), FWEditServiseContentActivity.class);
                        intent.putExtra("id",id1);
                        intent.putExtra("from","1");
                        startActivity(intent);
                    }
                });


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(getActivity(),ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }



    // 删除某一服务
    private void deleteServiceItem(final String position, final int poi) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.SHOPDELETESERVICE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject deleteobj = new JSONObject();
        deleteobj.put("serviceId",position);
        params.setBodyContent(deleteobj.toJSONString());

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                adapter.list.remove(poi);
                adapter.notifyDataSetChanged();
                try {
                    selllist.turnToNormal();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                selllist.invalidate();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(getActivity(),ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

}
